Skip to content

Kanban #1794: Merge into tables with triggers keeps omitted columns and fires update triggers - #8317

Open
labkey-jeckels wants to merge 10 commits into
developfrom
fb_merge_trigger_padding_1794
Open

labkey-jeckels wants to merge 10 commits into
developfrom
fb_merge_trigger_padding_1794

Conversation

@labkey-jeckels

@labkey-jeckels labkey-jeckels commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Rationale

This PR stops merges into tables with trigger scripts from clearing the columns the incoming data leaves out, and makes script triggers treat a merged row that matches an existing row as an update. The GH Issue 1048 fix only disabled merge for the Biologics registry and media tables; Kanban #1794 tracks the general fix. Whether EHR triggers opt out of the update routing is still open on that issue.

Related Pull Requests

Changes

  • Merges into tables with triggers fill each omitted column with the existing row's value, so triggers see the whole row and the omitted columns keep their values; new rows still get nulls, and replace still nulls omitted columns.
  • Script triggers receive merged and replaced rows that match an existing row through the update hooks, with the existing row as the old row; rows with new keys still use the insert hooks. Java triggers still use the insert hooks.
  • Triggers can find the existing row for lists, study datasets, and other tables, not just sample types and data classes, without changing the rest of the import.
  • On replace, trigger-managed columns the incoming data omits keep their values on existing rows.
  • A trigger that copies an existing row's unchanged value into the row doesn't need to declare that column as managed.
  • Integration tests for lists, sample types, and study datasets.

…ting row

Script triggers now get beforeUpdate/afterUpdate with the existing row for merge and replace rows whose key already exists, on lists, sample types, data classes, and study datasets.
…adding_1794

# Conflicts:
#	api/src/org/labkey/api/dataiterator/ExistingRecordDataIterator.java
#	list/src/org/labkey/list/ListModule.java
#	list/src/org/labkey/list/model/ListQueryUpdateService.java

// Rows routed to update get the whole existing row as oldRow
di = ExistingRecordDataIterator.createBuilder(di, _target, existingRecordKeyColumnNames, sharedKeys, _target instanceof ExpTable, routeMerge).getDataIterator(context);
DataIterator before = new BeforeIterator(new CachingDataIterator(di), context, carryForwardColumns);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude:

nit: on a routed MERGE, dataset and list trigger rows now include the routing lookup's existing-record column, and datasets also get the computed lsid. Probably harmless, since ExpTable triggers already see the former, but worth knowing if any script iterates over row's keys.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed that this seems harmless. Leaving as-is.

Object date = dateIndex == null ? null : input.get(dateIndex);
if (participant != null && (_dsd.isDemographicData() || sequenceNum instanceof Number))
{
return lsid.translateLsid(participant.toString(), sequenceNum instanceof Number n ? n.doubleValue() : null,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude:

When the input has a Container column, this builds the LSID from it, but dataset import ignores that column unless the study is a dataspace study with TargetMultipleContainers (DatasetDataIteratorBuilder:186-196). If the value isn't the folder's entityId, such as a folder path, the LSIDs never match. The row's trigger then fires beforeInsert with no old row while import updates the existing row.

Reproduced this locally: a MERGE with Container set to the folder path updated the existing row but fired beforeInsert. Could this pass the container only in that dataspace/TargetMultipleContainers case, matching import?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, and test case added.


// The import pipeline doesn't expect an existing record from a lookup made only to route triggers
boolean routingOnlyLookup = false;
if (routeMerge && existingRecordKeyColumnNames == null)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Manual testing: a blank key in a list MERGE returns 500 when the list has a script trigger

Repro:

  1. Create a list MergeBlankKey with a Text key Key and a Text field Val, and insert one row with Key = K000.
  2. In an enabled module, add a trigger script queries/lists/MergeBlankKey.js containing function beforeInsert(row, errors) {}.
  3. From the browser console in that folder, run:
LABKEY.Query.importData({
    schemaName: 'lists', queryName: 'MergeBlankKey', format: 'tsv', insertOption: 'MERGE',
    text: 'Key\tVal\nK200\ta\n\tb\nK000\tc',
    success: console.log, failure: console.error
});

Expected (develop): the row error Missing value for required property: Key on row 2.

Actual (this branch): HTTP 500 "Should not be here". The server log shows PSQLException: No value specified for parameter 3 from ExistingDataIteratorsTableInfo.prefetchExisting. Without the trigger script, this branch returns the expected row error.

The routing lookup added here runs before the list's required-field check. getSelectExistingSql appends the key's ? placeholder before its null check, then continues without binding a value. That bug predates this PR, but lists with triggers didn't reach it before. Checking for null before appending the placeholder fixes it; ExistingDataIteratorsGetRows already returns early.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related: a duplicate key hits a similar 500. A list MERGE with a script trigger and K000 twice fails with an assertion error at ExistingRecordDataIterator.get; develop, and the same list without a trigger, return Duplicate key provided: K000. The routing lookup records the duplicate error first, so the import's own lookup skips its prefetch and the after-trigger getMap() reads an empty record. Skipping unmatched keys in the routing-only lookup, instead of adding row errors, would fix both this and the blank-key 500.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test cases added and underlying problems fixed.

…adding_1794

# Conflicts:
#	experiment/src/org/labkey/experiment/ExperimentModule.java
…s see the whole row

Merge pads omitted columns with each matched row's stored value instead of leaving them out, so existing triggers that fill or copy omitted columns keep working.
…as row errors

Moves the stored-vs-submitted value comparison into ValueUtil for managed-column checks, and consolidates the list and dataset merge tests onto shared helpers.
@labkey-jeckels
labkey-jeckels marked this pull request as ready for review November 29, 2026 22:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants